package com.wjjumpad.cwjjumpad.module;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.app.ActivityManager;
import android.content.Context;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

import com.wjjumpad.cwjjumpad.R;
import com.wjjumpad.cwjjumpad.adapter.AppInfoAdapter;
import com.wjjumpad.cwjjumpad.base.QLBaseActivity;
import com.wjjumpad.cwjjumpad.model.AppInfoModel;

import java.util.ArrayList;
import java.util.List;

public class InstalledAppListActivity extends QLBaseActivity {

    private TextView joinText;

    private ListView listView;

    private Button getAppListBtn;

    private Boolean isHide = false;

    private RecyclerView getAppinfoList;

//    public static void startActivity(Context context, Order order) {
//        Intent intent = new Intent(context, OrderDetailActivity.class);
//        intent.putExtra(EXTRA_ORDER, order);
//        context.startActivity(intent);
//    }

    @Override
    public int getLayout() {
        return R.layout.activity_installed_app_list;
    }

    @Override
    public void init() {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_installed_app_list);
        setActionTitle("安装列表");
        showRightImg();

        findViews();
        initViews();
    }

    @Override
    public View initSuccessView() {
        return null;
    }

    @Override
    public void actionbarDoBackOnclick() {
        finish();
    }

    @Override
    public void actionbarRightImgOnclick() {
//        super.actionbarRightImgOnclick();

        Log.d("applicationList", "actionbarRightImgOnclick: ");

        hideAppWindow(this);

    }

    public void hideAppWindow(Context context){
        try {
            ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
            //控制App的窗口是否在多任务列表显示
            List<ActivityManager.AppTask> appTasksList = activityManager.getAppTasks();
            for (ActivityManager.AppTask appTask : appTasksList) {
                Log.d("applicationList", "hideAppWindow - isHide:"+isHide);
                isHide = !isHide;
                Log.d("applicationList", "hideAppWindow - appTask: "+appTask+"   isHide:"+isHide);

                // 在多任务界面隐藏app
                appTask.setExcludeFromRecents(isHide);
            }
        }catch (Exception e){

        }
    }

//    //在多任务列表页面隐藏App窗口
//    fun hideAppWindow(context: Context,isHide:Boolean) {
//        try {
//            val activityManager:ActivityManager = context.getSystemService(Context.ACTIVITY_SERVICE)
//            as ActivityManager
//            //控制App的窗口是否在多任务列表显示
//            activityManager.appTasks[0].setExcludeFromRecents(isHide)
//        } catch (e:Exception){
//            .....
//        }
//    }


        private void findViews(){

        getAppinfoList = findViewById(R.id.appinforecyclerview);
//        joinText = findViewById(R.id.join_text);

//        listView = findViewById(R.id.list_view);

//        getAppListBtn = findViewById(R.id.get_app_list_btn);
    }

    private void initViews() {
        // 获取系统的包管理器
        PackageManager packageManager = getPackageManager();

        ArrayList<String>  nameData = new ArrayList<String>();
        List<AppInfoModel> appInfoModelList = new ArrayList<>();
        List<ApplicationInfo> applicationList = getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
        for (ApplicationInfo info : applicationList) {
            Log.d("applicationList", "ApplicationInfo: "+info);
            //获取应用图标
            Drawable appicon = info.loadIcon(packageManager);

            Log.d("applicationList", "appicon: "+appicon);

            //获取应用程序的 包名
            String appPackageName = info.packageName;

            // 获取应用名
            // 参数 packageManager  是应用管理者对象
            String appName = info.loadLabel(packageManager).toString();
            nameData.add(appName);

            //

            AppInfoModel appinfo = new AppInfoModel(
                    appicon,
                    appName,
                    "123"
            );
            appInfoModelList.add(appinfo);
        }

        //参数 3：数据源
//        ArrayAdapter<String> adapter = new ArrayAdapter<String>(InstalledAppListActivity.this,android.R.layout.simple_list_item_1,nameData);

        AppInfoAdapter adapter = new AppInfoAdapter(appInfoModelList);
        getAppinfoList.setAdapter(adapter);

        LinearLayoutManager layoutManager = new LinearLayoutManager(this);
        layoutManager.setOrientation(RecyclerView.VERTICAL);
        getAppinfoList.setLayoutManager(layoutManager);
//
//        listView.setAdapter(adapter);
//        setOnClick(getAppListBtn,new View.OnClickListener() {
//                 @Override
//                 public void onClick(View v) {
//
//                 }
//             });

//        getAppListBtn.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                Log.d("getAppListBtn", "onClick: ");
//
//                // 获取系统的包管理器
//                PackageManager packageManager = getPackageManager();
//                // 查询所有已安装的应用程序信息
//                List<PackageInfo> installedPackages = packageManager.getInstalledPackages(PackageManager.GET_META_DATA);
//                Log.d("PackageManager", "installedPackages: "+installedPackages.toString());
////                List<Fruit> fruitlist = new ArrayList<>();
////                for (int i = 0;i<installedPackages.size();i++){
////                    PackageInfo  info = installedPackages[i];
////
////                }
//
//                for (PackageInfo info : installedPackages) {
//                    Log.d("installedPackages", "PackageInfo: "+info);
//                }
//
//
//                ArrayList<String>  nameData = new ArrayList<String>();
//                List<ApplicationInfo> applicationList = getPackageManager().getInstalledApplications(PackageManager.GET_META_DATA);
//                for (ApplicationInfo info : applicationList) {
//                    Log.d("applicationList", "ApplicationInfo: "+info);
//                    //获取应用图标
//                    Drawable drawable = info.loadIcon(packageManager);
//
//                    //获取应用程序的 包名
//                    String appPackageName = info.packageName;
//
//                    //获取应用名
//                    //参数 packageManager  是应用管理者对象
//                    String appName = info.loadLabel(packageManager).toString();
//                    nameData.add(appName);
//                }
//
//                Log.d("applicationList", "nameData: "+nameData);
//
//                ArrayList<ApplicationInfo> arrayList = new ArrayList<>(applicationList);
//
////                for (PackageInfo e : installedPackages) {
////                    installedPackages.add(e.activityInfo.packageName);
////                }
//                //参数 3：数据源
//                ArrayAdapter<String> adapter = new ArrayAdapter<String>(InstalledAppListActivity.this,android.R.layout.simple_list_item_1,nameData);
////
//                listView.setAdapter(adapter);
//            }
//        });

//        getAppListBtn.setOnClickListener(new );
    }
}